<template>
  <div class="hzy_setgroup">
    <!-- 设置分组表格 -->
    <el-dialog title="设置分组" :visible.sync="dialogTableVisible">
      <el-button type="primary" @click="innerVisible = true"
        >新建分组</el-button
      >
      <el-dialog
        width="25%"
        title="新建分组"
        :visible.sync="innerVisible"
        append-to-body
      >
        <!-- 新增分组 -->
        <el-form
          :label-position="labelPosition"
          label-width="80px"
          :model="setNewGroup"
        >
          <el-form-item label="分组名称">
            <el-input v-model="setNewGroup.zuMing"></el-input>
          </el-form-item>
          <el-form-item label="分组备注">
            <el-input v-model="setNewGroup.banCi"></el-input>
          </el-form-item>
          <el-form-item label="所属门店">
            <el-input v-model="setNewGroup.fenDianName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取消</el-button>
          <el-button :plain="true" type="primary" @click="saveAddGroup"
            >保存</el-button
          >
        </span>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">关闭</el-button>
      </div>
      <!-- 分组表格 -->
      <el-table :data="setData" border style="width: 700px; margin-top: 20px">
        <el-table-column prop="id" label="序号" width="150"> </el-table-column>
        <el-table-column prop="zuMing" label="分组名称" width="120">
        </el-table-column>
        <el-table-column prop="banCi" label="分组备注" width="120">
        </el-table-column>
        <el-table-column prop="fenDianName" label="所属门店" width="120">
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button @click="editClick(scope.row)" type="success" size="small"
              >编辑</el-button
            >
            <el-button
              @click="delGroupClick(scope.row.id)"
              type="danger"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="sizes, prev, pager, next"
        :total="totalCountNum"
      >
      </el-pagination>
      <!-- 编辑分组 -->
      <el-dialog
        width="25%"
        title="编辑分组"
        :visible.sync="innerEdit"
        append-to-body
      >
        <el-form
          :label-position="labelPosition"
          label-width="80px"
          :model="groupList"
        >
          <el-form-item label="分组名称">
            <el-input v-model="groupList.zuMing"></el-input>
          </el-form-item>
          <el-form-item label="分组备注">
            <el-input v-model="groupList.banCi"></el-input>
          </el-form-item>
          <el-form-item label="所属门店">
            <el-input v-model="groupList.fenDianName"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerEdit = false">取消</el-button>
          <el-button :plain="true" type="primary" @click="SaveEditGroup"
            >保存</el-button
          >
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  getGroupListApi,
  delGroupApi,
  editGroupApi,
  addGroupApi,
} from "@/http/employeeListApi";
export default {
  name: "SetEmployeeGroup",
  props: ["isShowSet"],
  data() {
    return {
      setData: [],
      dialogTableVisible: false, // 设置分组界面显示
      innerVisible: false, // 新建分组界面显示
      innerEdit: false, // 编辑分组界面显示
      labelPosition: "right",
      setNewGroup: {
        // 添加分组
        zuMing: "",
        banCi: "",
        fenDianName: "",
      },
      formLabelWidth: "120px",
      groupTotalCount: 20, // 分组总数量
      groupList: {}, // 编辑分组
      addGroup: {}, //新增分组
      page: 1, // 当前页
      size: 5, //当前页面展示的数量
      totalCountNum: 20, // 分组总数量
    };
  },
  created() {
    this.getGroupInit(this.page, this.size);
  },
  mounted() {
    this.dialogTableVisible = this.isShowSet;
  },
  watch: {
    dialogTableVisible: function () {
      if (!this.dialogTableVisible) {
        this.$emit("closeSet");
      }
    },
  },
  methods: {
    handleSizeChange(size) {
      // 每页展示的数量
      console.log("每页" + size + "条");
      this.size = size;
      this.getGroupInit(this.page, size);
    },
    handleCurrentChange(page) {
      // 当前分页
      console.log("当前页:", page);
      this.page = page;
      this.getGroupInit(page, this.size);
    },
    getGroupInit(page, size) {
      // 获取分组数据
      // getGroupTotal()
      //   .then((res) => {
      //     this.groupTotalCount = res.data.data;
      //   })
      //   .catch((err) => {
      //     console.log("获取总数量失败：", err);
      //   });
      getGroupListApi(page, size)
        .then((res) => {
          this.setData = res.data.data;
          console.log(this.setData);
        })
        .catch((err) => {
          console.log("获取列表失败：", err);
        });
    },
    editClick(row) {
      // 编辑分组界面显示
      console.log(row);
      this.innerEdit = true;
      this.groupList = row;
      console.log(this.groupList);
    },
    SaveEditGroup() {
      // 保存编辑后的分组
      this.innerEdit = false;
      editGroupApi(this.groupList)
        .then((res) => {
          console.log(this.groupList);
          this.getGroupInit(this.page, this.size); // 重新渲染
          console.log("编辑成功：", res);
          this.$message({
            message: "保存成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log("编辑失败：", err);
        });
    },
    delGroupClick(id) {
      // 删除某个分组
      console.log("删除的id是：", id);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          this.delGroup(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    delGroup(id) {
      delGroupApi(id)
        // 删除请求
        .then((res) => {
          console.log("删除成功：", res);
          this.getGroupInit(this.page, this.size);
        })
        .catch((err) => {
          console.log("删除失败：", err);
        });
    },
    saveAddGroup() {
      // 保存新增后的分组
      this.addGroup = this.setNewGroup;
      console.log(this.addGroup);
      this.innerVisible = false;

      addGroupApi(this.addGroup)
        .then((res) => {
          this.setNewGroup = {}; // 制空
          this.getGroupInit(this.page, this.size); //重新渲染
          console.log("新增成功：", res);
          this.$message({
            message: "保存成功",
            type: "success",
          });
        })
        .catch((err) => {
          console.log("新增失败：", err);
        });
    },
    SetInfo() {
      // 设置分组界面显示
      this.$emit("setGroup");
      this.dialogTableVisible = true;
    },
  },
};
</script>